package edu.ustb.order.controller;

import edu.ustb.order.entity.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/order")
public class OrderController {

    //注入restTemplate对象
    @Autowired
    private RestTemplate restTemplate;

    /**
     * 注入DiscoveryClient :
     *  springcloud提供的获取原数组的工具类
     *      调用方法获取服务的元数据信息
     *
     */
    @Autowired
    private DiscoveryClient discoveryClient;


    /**
     * 基于ribbon的形式调用远程微服务
     *  1.使用@LoadBalanced声明RestTemplate
     *  2.使用服务名称替换ip地址
     */
    @RequestMapping(value = "/buy/{id}",method = RequestMethod.GET)
    public Product findById(@PathVariable Long id) {
        Product product = null;
        product = restTemplate.getForObject("http://service-product/product/1",Product.class);
        return product;
    }

    /**
     * 参数:商品id
     *  通过订单系统,调用商品服务根据id查询商品信息
     *      1.需要配置商品对象
     *      2.需要调用商品服务
     *  使用java中的urlconnection,httpclient,okhttp
     */
//	@RequestMapping(value = "/buy/{id}",method = RequestMethod.GET)
//	public Product findById(@PathVariable Long id) {
//		//调用discoveryClient方法
//		//已调用服务名称获取所有的元数据
//		List<ServiceInstance> instances = discoveryClient.getInstances("service-product");
//		//获取唯一的一个元数据
//		ServiceInstance instance = instances.get(0);
//		//根据元数据中的主机地址和端口号拼接请求微服务的URL
//		Product product = null;
//		//如何调用商品服务?
//		product = restTemplate.getForObject("http://"+instance.getHost()+":"+instance.getPort()+"/product/1",Product.class);
//		return product;
//	}

    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public String findOrderById(@PathVariable Long id){
        return "调用订单系统查询订单";
    }

    @GetMapping("/getEurekaServices")
    public List<String> getEurekaServices(){
        List<String> services = new ArrayList<>();
        List<String> serviceNames = discoveryClient.getServices();
        for(String serviceName : serviceNames){
            List<ServiceInstance> serviceInstances = discoveryClient.getInstances(serviceName);
            for(ServiceInstance serviceInstance : serviceInstances){
                services.add(String.format("%s:%s",serviceName,serviceInstance.getUri()));
            }
        }
        return services;
    }

}
